package com.chpro.http.yixin.auth;

import com.chpro.common.FileUtil;
import com.chpro.common.HttpUtil;
import com.chpro.common.JSONUtils;
import com.chpro.http.yixin.servlet.ExtendActionServlet;

/**
 * Created by Administrator on 14-10-2.
 */
public class EasyAccessTokenService {
    private static final String TO_KEN_FILE = "yixin/toKen.properties";  //易信toKen配置文件

    public void setAccountToken() throws Exception{
        String grant_type = "client_credential";
        String appid = ExtendActionServlet.commonConfig.getString("appid");
        String secret = ExtendActionServlet.commonConfig.getString("secret");
        String url = "https://api.yixin.im/cgi-bin/token";

        String bodyStr = HttpUtil.httpsGet(url+"?grant_type="+grant_type+"&appid="+appid+"&secret="+secret);
        ToKenModel toKenModel = (ToKenModel)JSONUtils.toObject(bodyStr,ToKenModel.class);
        String content = "access_token="+toKenModel.getAccess_token();
        FileUtil.writeClassFile(TO_KEN_FILE,content);
    }

    public static void main(String[] args)throws Exception{
        ExtendActionServlet extendActionServlet = new ExtendActionServlet();
        extendActionServlet.init();
        EasyAccessTokenService easyAccessTokenService = new EasyAccessTokenService();
        easyAccessTokenService.setAccountToken();
    }
}
